package com.itheima.project.controller;

import com.alibaba.fastjson.JSONObject;
import com.itheima.project.adepter.LoginAuthAdepter;
import com.itheima.project.basic.ResponseWrap;
import com.itheima.project.core.enums.basic.BasicEnum;
import com.itheima.project.handler.LogoutHandler;
import com.itheima.project.handler.RefreshTokenHandler;
import com.itheima.project.utils.ResponseWrapBuild;
import com.itheima.project.utils.SubjectContent;
import com.itheima.project.vo.security.EnterpriseVo;
import com.itheima.project.vo.security.UserVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import java.security.Principal;
import java.util.Map;

/**
 * 自定义Oauth2获取令牌接口
 */
@RestController
@RequestMapping("/oauth")
@Api(tags = "登录认证controller")
public class AuthController {

    @Autowired
    private LoginAuthAdepter loginAuthAdepter;

    @Autowired
    private LogoutHandler logoutHandler;

    @Autowired
    private RefreshTokenHandler refreshTokenHandler;

    /**
     * Oauth2登录认证
     * 必传：
     * client_id：客户端
     * client_secret：客户端秘钥
     * grant_type：固定传入：password字符串
     * login_type：OauthConstant中选择：USER_USERNAME、USER_MOBILE、CUSTOMER_USERNAME、CUSTOMER_MOBILE
     * username:用户账号
     * password:用户密码
     */
    @RequestMapping(value = "/token", method = RequestMethod.POST)
    @ApiOperation(value = "Oauth2登录认证",notes = "Oauth2登录认证")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "grant_type", value = "授权模式",  example = "password",required = true),
            @ApiImplicitParam(name = "client_id",  value = "Oauth2客户端ID",example = "operators-pc", required = true),
            @ApiImplicitParam(name = "client_secret",value = "Oauth2客户端秘钥",  example = "pass", required = true),
            @ApiImplicitParam(name = "username",  value = "登录用户名",example = "admin@qq.com"),
            @ApiImplicitParam(name = "mobile", value = "登录用户名", example = "15156403088"),
            @ApiImplicitParam(name = "password", value = "登录密码", example = "pass"),
            @ApiImplicitParam(name = "login_type", value = "登录类型", example = "user-username,user-mobile,user-wechat"),
            // 微信认证参数
            @ApiImplicitParam(name = "code", value = "小程序code"),
    })
    public ResponseWrap<UserVo> postAccessToken(@ApiIgnore Principal principal,
                                                @ApiIgnore @RequestParam Map<String, String> parameters)
            throws HttpRequestMethodNotSupportedException {
        UserVo userVo = loginAuthAdepter.adepterRoutes(principal, parameters);
        return ResponseWrapBuild.build(BasicEnum.SUCCEED, userVo);
    }

    /***
     * @description 使用用户jti刷新令牌，前端可以根据情况进行刷新
     *  必传：
     *  client_id：客户端
     *  client_secret：客户端秘钥
     *  grant_type：固定传入：refresh_token字符串
     *  jti:本次回话标识
     * @return
     */
    @ApiOperation(value = "Oauth2登录令牌刷新",notes = "Oauth2登录令牌刷新")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "principal",value = "主要令牌对象",required = false,dataType = "CustomerVo"),
            @ApiImplicitParam(name = "parameters",value = "登录参数",required = true,dataType = "Map"),
    })
    @RequestMapping(value = "/refresh-token", method = RequestMethod.POST)
    public ResponseWrap<Boolean> refreshToken(Principal principal,
                                              @RequestParam Map<String, String> parameters) {
        Boolean isRefresh = refreshTokenHandler.refreshToken(principal, parameters);
        return ResponseWrapBuild.build(BasicEnum.SUCCEED, isRefresh);
    }

    /***
     * @description 退出接口
     * @return
     */
    @ApiOperation(value = "Oauth2退出登录",notes = "Oauth2退出登录")
    @RequestMapping(value = "/logout", method = RequestMethod.POST)
    public ResponseWrap<Boolean> logout() {
        String subject = SubjectContent.getSubject();
        UserVo userVo = JSONObject.parseObject(subject, UserVo.class);
        Boolean flag = logoutHandler.logout(userVo.getJti());
        return ResponseWrapBuild.build(BasicEnum.SUCCEED, flag);
    }

    /***
     * @description 按host查询企业相关信息
     * @return
     * @return: com.itheima.project.vo.security.EnterpriseVo
     */
    @ApiOperation(value = "微信登录企业信息查询",notes = "微信登录企业信息查询")
    @RequestMapping(value = "/enterpriseVo-host", method = RequestMethod.GET)
    public ResponseWrap<EnterpriseVo> findEnterpriseVoByHost(){
        EnterpriseVo enterpriseVo = loginAuthAdepter.findEnterpriseVoByHost();
        return ResponseWrapBuild.build(BasicEnum.SUCCEED, enterpriseVo);
    }
}
